package com.liuhouse.strame;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class OptionalDemo {

    public static void main(String[] args) {
        //        if(author != null){
//            System.out.println(author.getName());
//        }else{
//            System.out.println("数据是空的");
//        }

//        Author author = getAUTHOR();
//        Optional<Author> author1 = Optional.ofNullable(author);
//        author1.ifPresent(author11 ->
//            System.out.println(author11.getName())
//        );


        //直接获取optional对象
//        Optional<Author> authorOptional = getAuthorOptional();
//        authorOptional.ifPresent(author -> System.out.println(author.getName()));


//        orElseGetTest();
//        orElseThrowTest();

//        testFiltersss();


//        ifPresentTest();

        //数据转换
        OptionalDemo optionalDemo = new OptionalDemo();
        List<Book> books = optionalDemo.testDataChange();
        System.out.println(books);

    }

    public List<Book> testDataChange() {
        //Optional还提供了map可以让我们对数据进行转换,并且转换得到的数据也还是被Optional
        //包装好的，保证了我们的使用安全
        //例如我们想获取作家的书籍集合
        Optional<Author> optionalAuthor = Optional.ofNullable(getAUTHOR());
        List<Book> books = optionalAuthor.
                map(new Function<Author, List<Book>>() {
                    @Override
                    public List<Book> apply(Author author) {
                        return author.getBooks();
                    }
                }).orElseGet(new Supplier<List<Book>>() {
                    @Override
                    public List<Book> get() {
                        return null;
                    }
                });

        return books;

//        Optional<Author> authorOptional = Optional.ofNullable(getAUTHOR());
//        Optional<List<Book>> books = authorOptional.map(author -> author.getBooks());
//       books.ifPresent(books1 -> books1.forEach(book -> System.out.println(book.getName())));
    }

    private static void ifPresentTest() {
        Optional<Author> authorOptional = Optional.ofNullable(getAUTHOR());
        //如果当前的对象不是空的话
        if(authorOptional.isPresent()){
            System.out.println(authorOptional.get().getName());
        }
    }

    private static void testFiltersss() {
        Optional<Author> authorOptional = Optional.ofNullable(getAUTHOR());
        authorOptional.filter(author -> author.getAge()>18
        ).ifPresent(author1 -> System.out.println(author1.getName()));
    }

    private static void orElseThrowTest() {
        Optional<Author> authorOptional = getAuthorOptional();
        try {
            Author author = authorOptional.orElseThrow((Supplier<Throwable>) () -> new RuntimeException("数据为null"));
            System.out.println(author);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }


    //安全获取值
    private static void orElseGetTest() {
        Optional<Author> authorOptional = Optional.ofNullable(getAUTHOR());
        //使用get获取数据
//        Author author = authorOptional.get();
//        System.out.println(author);

        //使用orElseGet安全的获取值
//        Author author = authorOptional.orElseGet(new Supplier<Author>() {
//            @Override
//            public Author get() {
//                //如果当前对象是null的时候 给返回一个对象
//                return new Author(0L, "空值", 0, "空介绍", null);
//            }
//        });
//        System.out.println(author.getName());

        Author author = authorOptional.orElseGet(() -> new Author(0L, "空值", 0, "空介绍", null));
        System.out.println(author.getName());

    }


    public static Author getAUTHOR(){
        //书籍列表
        ArrayList<Book> books1 = new ArrayList<>();

        //给书籍添加数据
        books1.add(new Book(1L , "刀的两侧是光明与黑暗11","哲学,爱情11" ,85 ,"一刀就会两段啊11"));
        books1.add(new Book(2L , "刀的两侧是光明与黑暗22","个人成长,爱情22" ,90 ,"一刀就会两段啊222"));
        Author author = new Author(1L, "蒙多", 33, "一个从才阿德怕怕都怕", books1);
        return author;
    }


    public static Optional<Author> getAuthorOptional(){
        Author author = new Author(2L, "降央卓玛", 55, "爱江山更爱美人", null);
        Optional<Author> authorOptional = Optional.ofNullable(null);
        //如果你确定一个对象不是空的 则可以使用Optional的静态方法of来把数据封装成Optional对象
//        Optional<Author> authorOptional = Optional.of(null);
        return authorOptional;
    }


}
